package at.fhooe.engine.graphics;

import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;

public class FullScreen implements Screen {

	private static final int BIT_DEPTH = 32;
	private GraphicsDevice m_device = null;
	private Frame m_mainFrame = null;
	private BufferStrategy m_bufferStrategy = null;
	
	@Override
	public void open(int width, int height) {
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        this.m_device = env.getDefaultScreenDevice();
        
		GraphicsConfiguration gc = m_device.getDefaultConfiguration();
		
        m_mainFrame = new Frame(gc);	
        m_mainFrame.setIgnoreRepaint(true);
		m_mainFrame.setUndecorated(true);
        m_device.setFullScreenWindow(m_mainFrame);
        if(m_device.isDisplayChangeSupported()) {
        	m_device.setDisplayMode(new DisplayMode(width, height, BIT_DEPTH, DisplayMode.REFRESH_RATE_UNKNOWN));
        }
        m_mainFrame.createBufferStrategy(2);
        m_bufferStrategy = m_mainFrame.getBufferStrategy();
	}

	@Override
	public void close() {
		m_device.setFullScreenWindow(null);
		System.exit(0);
	}

	@Override
	public Graphics2D beginUpdate() {
		return (Graphics2D) m_bufferStrategy.getDrawGraphics();
	}

	@Override
	public void endUpdate() {
		m_bufferStrategy.show();
	}

	@Override
	public void addKeyListener(KeyListener keyListener) {
		m_mainFrame.addKeyListener(keyListener);
	}

	@Override
	public void addMouseListener(MouseListener mouseListener) {
		m_mainFrame.addMouseListener(mouseListener);
	}

	@Override
	public void addMouseMotionListener(MouseMotionListener mouseMotionListener) {
		m_mainFrame.addMouseMotionListener(mouseMotionListener);
	}
}
